<?php
/**
 * Created by PhpStorm.
 * User: wo
 * Date: 2019/9/3
 * Time: 15:38
 */

namespace Api\Controller;


use Think\Controller;

class PastController extends MapiBaseController
{
    public function paste(){
        $GLOBALS['HTTP_RAW_POST_DATA'] = stripslashes($GLOBALS['HTTP_RAW_POST_DATA']);
        $GLOBALS['HTTP_RAW_POST_DATA'] = str_replace('"[','[',$GLOBALS['HTTP_RAW_POST_DATA']);
        $GLOBALS['HTTP_RAW_POST_DATA'] = str_replace(']"',']',$GLOBALS['HTTP_RAW_POST_DATA']);
        $_POST = json_decode($GLOBALS['HTTP_RAW_POST_DATA'],true);
        if(!$_POST['SkuList'] || !is_array($_POST['SkuList'])){
            ApiReturn(2,'','参数错误');
        }
        //查询商品信息
        $goods_model = M('goods');
        $list = array();
        foreach($_POST['SkuList'] as $k=>$v){
            $list[$k] = $goods_model->where(array('sku'=>$v))->find();
        }
        //遍历商品信息 匹配已存在的sku
        $newArr = array();
        foreach($list as $key=>$value){
            if($value['sku'] == $_POST['SkuList'][$key]){
                $newArr[] = $value['sku'];
            }
        }
        //拼接参数请求oms接口
        $url = C('OMS_URL').'/api/Product/GetproductPrice';
        $data['ClientGuid'] = C("CLIENT_GUID");
        $data['ClientName'] = C("CLIENT_NAME");
        $data["PriceChange"]['SkuList'] = $newArr;
        $this->writeLog('收到传入的sku查询结果：'.json_encode($data['PriceChange']['SkuList']),'UpdateProductPrice');
        if(!empty($data["PriceChange"]['SkuList'])){
            $this->writeLog('发送oms接口地址：'.$url .'参数：：'.json_encode($data),'UpdateProductPrice');
            $result = sync_data($url,$data);
            $this->writeLog('发送oms接口地址：'.$url.' 接口返回结果：'.json_encode($result),'UpdateProductPrice');
            if($result['code'] == 200){
                $this->saveProductPrice($result['data']);
                render(1,'');
            }else{
                render(2,'','请求失败！');
            }
        }
        render(2,'未找到合适的SKU！');
    }

    /**
     * 更新product 、product_spec 价格
    */
    public function saveProductPrice($arr = array()){
        if(!is_array($arr) || empty($arr)){
            ApiReturn(2,'','数据错误');
        }
        $productModel = M("goods");
        $productSpecModel = M("goodsSpec");
        $myTax = M("my_tax");

        //遍历获取的sku
        $goodsList = array();
        foreach($arr as $k=>$v){
            //查询商品信息是否存在
            $result = $productModel->where(array('sku'=>$v['Sku']))->find();
            if($result){
                $productModel->where(array('sku'=>$v['Sku']))->save(array('market_price'=>$v['Price']));
//                $productModel->where(array('sku'=>$v['Sku']))->save(array('price'=>$v['Price']));
                $myTaxInfo = $myTax->where(array("tax_id"=>$result['tax_id']))->find();
            }
            if(!empty($v['WarehousePriceList'])){
                foreach($v['WarehousePriceList'] as $key=>$value){
                    $spec3value = $v['StoreId'].$value['WarehouseId'];
                    //查询对应仓库的规格是否存在
                    $specResult = $productSpecModel->where(array('goods_id'=>$result['goods_id'],'spec_3value'=>$spec3value))->find();
                    if($specResult){
                        $stockInfo = D("storestock")->where(array("storestock_id"=>$spec3value))->find();
                        $tax_price = 0.00;
                        if($stockInfo['stock_style'] != 3){
                            $tax_price = $v['Price'] * $myTaxInfo['consolidated_tax'];
                        }
                        $productSpecModel->where(array('goods_id'=>$result['goods_id'],'spec_3value'=>$spec3value))->save(array('price'=>$value['Price'],'tax_price'=>$tax_price));
                    }
                }
            }
        }
        return true;

    }

}